﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CoreData;
using ActionServices;

namespace ZidoniCMS.Controllers
{
    public class SearchController : BaseController
    {
        //
        // GET: /Search/

        public SearchController(ISearchActionService search, ICategoryActionService category):
            base(search, category){}
        
        public ActionResult Index(string value = "", int page = 1, int pageSize = 12)
        {
            var data = SearchService.SearchAll(value).ToList();
            if (data != null && data.Count() > 0)
            {
                ViewBag.CountItem = data.Count();
                ViewBag.page = page;
                ViewBag.PageSize = pageSize;
                ViewBag.TotalPage = (int)Math.Ceiling((float)data.Count / (float)pageSize);
                data = data.Skip((page - 1) * pageSize).Take(pageSize).ToList();
            }
            ViewBag.TitleName = value;
            return View(data);
        }
        /// <summary>
        /// Search Post back
        /// </summary>
        /// <param name="model">Search Model</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Index(Search model, int page = 1, int pageSize = 10)
        {
            if (model.Name != null && model.Name!="")
            {
                var data = SearchService.SearchAll(model.Name).ToList();
                if (data != null && data.Count() > 0)
                {
                    ViewBag.CountItem = data.Count();
                    ViewBag.page = page;
                    ViewBag.PageSize = pageSize;
                    ViewBag.TotalPage = (int)Math.Ceiling((float)data.Count / (float)pageSize);
                    data = data.Skip((page - 1) * pageSize).Take(pageSize).ToList();
                }
                ViewBag.TitleName = model.Name;
                return View(data);
            }
            else {
                return View();
            }
        }
        /// <summary>
        /// Dùng để cho chức năng search autocomplete
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetSearchJson(string value) {
            var data = SearchService.SearchByLimit(value, 10);
            foreach (var item in data) {
                if (item.Name.Length >= 25) {
                    item.Name = item.Name.Substring(0, 25) + "...";
                }
                if (item.Description.Length >= 35)
                {
                    item.Description = item.Description.Substring(0, 35) + "...";
                }
            }
            return Json(data, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// Search Input Control on Header
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public ActionResult _SearchInputControl()
        {
            return PartialView();
        }
    }
}
